package com.frontop.bigdata.modules.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.frontop.bigdata.modules.model.BigDataAccessPageInfo;
import com.frontop.bigdata.modules.model.BigDataProject;
import com.frontop.bigdata.modules.service.BigDataAccessPageInfoService;
import com.frontop.bigdata.modules.service.BigDataAccessPageService;
import com.frontop.bigdata.modules.service.BigDataProjectService;
import com.frontop.bigdata.modules.vo.BigDataAccessPageInfoVO;
import com.frontop.bigdata.modules.vo.BigDataProjectVO;
import com.frontop.common.domain.UserJurisdiction;
import com.frontop.common.util.JwtUtil;
import com.frontop.common.util.ResultJsonUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author YangJia
 * @since 2022-02-16
 */
@RestController
@RequestMapping("/big-data-access-page-info")
public class BigDataAccessPageInfoController {
    @Autowired
    BigDataAccessPageInfoService bigDataAccessPageInfoService;

    @Autowired
    BigDataProjectService bigDataProjectService;
    @Autowired
    BigDataAccessPageService bigDataAccessPageService;


    @ApiOperation(value = "网页列表")
    @PostMapping("/pageList")
    public ResultJsonUtil<Object> pageList(@RequestBody(required = false) BigDataAccessPageInfoVO vo,
                                           Authentication authentication,
                                           HttpServletRequest request) {
        Page<BigDataAccessPageInfo> page = new Page<>(vo.getPageNo(), vo.getPageSize());
        if(vo.getSiteId() == null){
            vo = new BigDataAccessPageInfoVO();
            UserJurisdiction userJurisdiction = JwtUtil.getUserJurisdiction(authentication);
            vo.setOrgId(userJurisdiction.getOrganizationId());
        }
        if(vo.getDateValue() != null){
            vo.setStartDate(vo.getDateValue().get(0));
            vo.setEndDate(vo.getDateValue().get(1));
        }
        page = bigDataAccessPageInfoService.selectBigDataAccessPageInfoList(page,vo);
        return new ResultJsonUtil().success(page);
    }


    @ApiOperation(value = "刷新所有受访页面当天数据")
    @GetMapping("/pullPage/{pullPageDate}")
    public ResultJsonUtil<Object> pullPage(
                                        @PathVariable("pullPageDate") String pullPageDate,
                                            Authentication authentication,
                                           HttpServletRequest request) {
        List<BigDataProject> bigDataProjectList = bigDataProjectService.list();
        for (BigDataProject bigDataProject : bigDataProjectList){
            bigDataAccessPageService.pullAccessPageData(bigDataProject.getSiteId(),pullPageDate);
        }
        return new ResultJsonUtil().success("刷新成功");
    }

    @ApiOperation(value = "修改网页信息")
    @PostMapping("/updatePage")
    public ResultJsonUtil<Object> updatePage(@RequestBody(required = false) BigDataAccessPageInfo page,
                                           Authentication authentication,
                                           HttpServletRequest request) {
        BigDataAccessPageInfo data = new BigDataAccessPageInfo();
        data.setPageId(page.getPageId());
        data.setPageName(page.getPageName());
        bigDataAccessPageInfoService.updateById(data);
        return new ResultJsonUtil().success("修改成功");
    }

    @ApiOperation(value = "删除网页")
    @GetMapping("/deletePage/{pageId}")
    @PreAuthorize("hasAuthority('bigdata:accessPageInfo:delete')")
    public ResultJsonUtil<Object> deletePage(@PathVariable("pageId") String pageId,
                                             Authentication authentication,
                                             HttpServletRequest request) {


        BigDataAccessPageInfo bigDataAccessPageInfo = new BigDataAccessPageInfo();
        bigDataAccessPageInfo.setPageId(pageId);
        bigDataAccessPageInfo.setStatus(1);
        bigDataAccessPageInfoService.updateById(bigDataAccessPageInfo);
        return new ResultJsonUtil().success("删除成功");
    }

}
